"""
核心模块 - 基础模型
提供项目中使用的基础模型类
"""
from django.db import models


class TimeStampedModel(models.Model):
    """
    带时间戳的基础模型
    自动创建时间和更新时间字段
    """
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        abstract = True


class SoftDeleteModel(models.Model):
    """
    软删除模型
    提供软删除功能
    """
    is_deleted = models.BooleanField('是否删除', default=False)
    deleted_at = models.DateTimeField('删除时间', null=True, blank=True)

    class Meta:
        abstract = True

    def soft_delete(self):
        """软删除"""
        from django.utils import timezone
        self.is_deleted = True
        self.deleted_at = timezone.now()
        self.save(update_fields=['is_deleted', 'deleted_at'])

    def restore(self):
        """恢复"""
        self.is_deleted = False
        self.deleted_at = None
        self.save(update_fields=['is_deleted', 'deleted_at'])


class BaseModel(TimeStampedModel, SoftDeleteModel):
    """
    基础模型
    包含时间戳和软删除功能
    """

    class Meta:
        abstract = True